package jhh.servlet;

import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jhh.entity.Jtl;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
@WebServlet("/BarChart")
public class BarChart extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		 //创建主题样式  
	       StandardChartTheme standardChartTheme=new StandardChartTheme("CN");  
	       //设置标题字体  
	       standardChartTheme.setExtraLargeFont(new Font("隶书",Font.BOLD,20));  
	       //设置图例的字体  
	       standardChartTheme.setRegularFont(new Font("宋书",Font.PLAIN,15));  
	       //设置轴向的字体  
	       standardChartTheme.setLargeFont(new Font("宋书",Font.PLAIN,15));  
	       //应用主题样式  
	       ChartFactory.setChartTheme(standardChartTheme); 
		response.setContentType("image/png");
		HttpSession session=request.getSession();
		List<Jtl> list=(List<Jtl>) session.getAttribute("jtl");
		int[]a=new int[10];
		for (Jtl jtl : list) {
			if(jtl.getT()<=200){
				if(jtl.isResult()==true){a[0]++;}
				else{a[1]++;}
			}else if(jtl.getT()<500){
				if(jtl.isResult()==true){a[2]++;}
				else{a[3]++;}
			}else if(jtl.getT()<1000){
				if(jtl.isResult()==true){a[4]++;}
				else{a[5]++;}
			}else if(jtl.getT()<2000){
				if(jtl.isResult()==true){a[6]++;}
				else{a[7]++;}
			}else {
				if(jtl.isResult()==true){a[8]++;}
				else{a[9]++;}
			}
		}
		CategoryDataset dataset=createDateset(a);
		JFreeChart jFreeChart=ChartFactory.createBarChart3D("响应时间分布", "响应时间", "线程个数", dataset,PlotOrientation.VERTICAL,true,true,false);
		CategoryPlot categoryPlot=(CategoryPlot) jFreeChart.getPlot();
		categoryPlot.setDomainGridlinesVisible(true);
		CategoryAxis categoryAxis=categoryPlot.getDomainAxis();
		categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.039269908169872414D));
		categoryAxis.setCategoryMargin(0.0D);
		BarRenderer3D barRenderer3D=(BarRenderer3D) categoryPlot.getRenderer();
		barRenderer3D.setDrawBarOutline(false);
		ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, 700, 300);
		
		
	}
	private CategoryDataset createDateset(int[]a){
		int k=0;
		DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		String[] cate1={"小于200","200-500","500-1000","1000-2000","大于2000"};
		String[] cate2={"成功","失败"};
		for(int i=0;i<cate1.length;i++){
			String category=cate1[i];
			for(int j=0;j<cate2.length;j++){
				String cat=cate2[j];
			   dataset.addValue(a[k], cat,category);
			   k++;
			}
		}
		return dataset;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
